//
//  UFOLQCHeroViewController.m
//  unrivalledForUniverse
//
//  Created by dllo on 1/13/16.
//  Copyright © 2016 dllo. All rights reserved.
//

#import "UFOLQCHeroViewController.h"
#import "UFOLQCLiveDetailsViewController.h"
#import "UFOLQCliveCollectionViewCell.h"
#import "LQCAFNetTool.h"
#import "UFOLQCLiveModel.h"

@interface UFOLQCHeroViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionView *mainCollectionV;
@property (nonatomic, retain) NSMutableArray *liveArr;

@end

@implementation UFOLQCHeroViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationItem.title = @"大神直播";
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
    //self.navigationController.navigationBar.hidden = YES;
    //self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLabel.backgroundColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.text = @"大神直播";
    self.navigationItem.titleView = titleLabel;
    [titleLabel sizeToFit];
    
    [self createCollectionV];
    [self createGetData];
    
    
}

- (void)createGetData
{
    [LQCAFNetTool getNetWithURL:@"http://www.quanmin.tv/json/categories/lol/list.json?01141551" body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *arr = [responseObject objectForKey:@"data"];
        self.liveArr = [UFOLQCLiveModel baseModelWithArr:arr];
        
        [self.mainCollectionV reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
}


- (void)createCollectionV{
    UICollectionViewFlowLayout *flow = [[[UICollectionViewFlowLayout alloc] init] autorelease];
   
    flow.minimumLineSpacing = 10 * SCREENHEIGHT / HEIGHTSIXP;
    flow.minimumInteritemSpacing = 10 * SCREENWIDTH / WIDTHSIXP;
    flow.headerReferenceSize = CGSizeMake(0, 0);
    flow.footerReferenceSize = CGSizeMake(0, 0);
    flow.itemSize = CGSizeMake(((SCREENWIDTH - 10 * 3 * SCREENWIDTH / WIDTHSIXP) / 2.0), ((SCREENWIDTH - 10 * SCREENWIDTH / WIDTHSIXP) / 3));
    flow.sectionInset = UIEdgeInsetsMake(10 * SCREENHEIGHT / HEIGHTSIXP, 10 * SCREENWIDTH / WIDTHSIXP, 10 * SCREENHEIGHT / HEIGHTSIXP, 10 * SCREENWIDTH / WIDTHSIXP);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.mainCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
    self.mainCollectionV.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
  
    self.mainCollectionV.delegate = self;
    self.mainCollectionV.dataSource = self;
    self.mainCollectionV.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.mainCollectionV];
    
    [_mainCollectionV release];
    
    [self.mainCollectionV registerClass:[UFOLQCliveCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.liveArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UFOLQCliveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    
    UFOLQCLiveModel *liveModel = [self.liveArr objectAtIndex:indexPath.item];
    
    NSURL *imageUrl = [NSURL URLWithString:liveModel.thumb];
    [cell.image sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"zanweitu"]];
    cell.titleLabel.text = liveModel.title;
    cell.numLabel.text = [NSString stringWithFormat:@"👤%@  ", liveModel.view];
    cell.nameLabel.text = liveModel.nick;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.hidesBottomBarWhenPushed=YES;
    
    UFOLQCLiveDetailsViewController *liveVC = [[[UFOLQCLiveDetailsViewController alloc] init]autorelease];
    UFOLQCLiveModel *liveM = [self.liveArr objectAtIndex:indexPath.item];
    
    liveVC.roomID = liveM.uid;
    
    [self presentViewController:liveVC animated:YES completion:^{
        
        
    }];
    
    self.hidesBottomBarWhenPushed=NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
